#include "toast.h"

Toast::Toast(const QString &text)
{
    // 设置基本属性
    this->setFixedSize(400, 150);
    this->setWindowFlags(Qt::FramelessWindowHint); // 设置窗口无边框
    this->setStyleSheet("QDialog { background-color:rgb(200, 200, 200); }");
    this->setAttribute(Qt::WA_DeleteOnClose);

    // 计算窗口的位置
    QScreen* screen = QApplication::primaryScreen(); // 拿到整个屏幕
    int x = screen->size().height() - 100 - this->height();
    int y = (screen->size().width() - this->width()) / 2;
    this->move(x, y);

    QVBoxLayout* layout = new QVBoxLayout();
    layout->setSpacing(0);
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setAlignment(Qt::AlignCenter);
    this->setLayout(layout);

    QLabel* label = new QLabel();
    label->setText(text);
    label->setStyleSheet("QLabel { font-size:30px; }");

    layout->addWidget(label);

    // 2s后自动关闭
    QTimer* timer = new QTimer(this);
    timer->start(2000);
    connect(timer, &QTimer::timeout, this, [=](){
        timer->stop();
        this->close();
    });
}

void Toast::makeToast(const QString &text)
{
    Toast* toast = new Toast(text);
    toast->exec();
}
